#ifndef _RTC_TIME_H_
#define _RTC_TIME_H_

#include "stdint.h"

#ifndef LIB_NO_TIME
#include "time.h"
#endif

#ifdef __cplusplus
extern "C"
{
#endif

#define RTC_SEC_PER_MIN 60L
#define RTC_MIN_PER_HOUR 60L
#define RTC_HOUR_PER_DAY 24L
#define RTC_MIN_PER_DAY (RTC_MIN_PER_HOUR * RTC_HOUR_PER_DAY)
#define RTC_SEC_PER_DAY (RTC_SEC_PER_MIN * RTC_MIN_PER_DAY)

	extern const uint8_t srtc_mon_day_p[12];
	extern const uint8_t srtc_mon_day_r[12];
	extern const uint16_t srtc_day_in_year_p[12];
	extern const uint16_t srtc_day_in_year_r[12];

	typedef struct
	{
		uint8_t sec;   
		uint8_t min;   
		uint8_t hour;  
		uint8_t week;  
		uint8_t day;   
		uint8_t mon;   
		uint16_t year; 
	} TIME_Type, rtc_type;

	typedef union
	{
		uint32_t val;
		struct
		{
			uint32_t sec2t : 5;
			uint32_t min : 6;
			uint32_t hour : 5;
			uint32_t day : 5;
			uint32_t mon : 4;
			uint32_t year : 7;
		} time;
	} sig_time_type;

	typedef union
	{
		uint32_t val;
		struct
		{
			uint32_t sec : 6;
			uint32_t min : 6;
			uint32_t hour : 5;
			uint32_t day : 5;
			uint32_t mon : 4;
			uint32_t year : 6;
		} time;
	} micro_time_type;

	typedef union
	{
		uint32_t val;
		struct
		{
			uint32_t min : 7;
			uint32_t hour : 6;
			uint32_t day : 6;
			uint32_t mon : 5;
			uint32_t year : 8;
		} time;
	} bcd_time_type;

	typedef union
	{
		uint16_t val;
		struct
		{
			uint16_t day : 5;
			uint16_t mon : 4;
			uint16_t year : 7;
		} time;
	} ymd_time_type;

	typedef union
	{
		uint32_t val;
		uint16_t wv[2];
		struct
		{
			uint8_t hour;
			uint8_t min;
			uint8_t sec;
			uint8_t ms10;
		} time;
	} hms_time_type;

	void TimeRun(rtc_type *TimeCur);
	void rtc_time_week_update(rtc_type *TimeCur);
	void rtc_time_run_sec(rtc_type *TimeCur, uint32_t sec_add);
	void rtc_time_run_min(rtc_type *TimeCur, uint32_t min_add);
	void rtc_time_run_hour(rtc_type *TimeCur, uint32_t hour_add);

	int32_t rtc_time_to_min(rtc_type *rtc);
	
	int32_t rtc_time_min_diff(rtc_type *rtc1, rtc_type *rtc2);

	int32_t rtc_time_sec_diff(rtc_type *rtc1, rtc_type *rtc2);
	
	int8_t TimeCompare(rtc_type *pTime1, rtc_type *pTime2);
	void TimeCopy(rtc_type *pTimeSrc, rtc_type *pTimeDes);

	bcd_time_type time_sig_to_bcd(sig_time_type sig_time);
	sig_time_type time_bcd_to_sig(bcd_time_type bcd_time);
	bcd_time_type time_to_bcd(rtc_type *time);
	sig_time_type time_to_sig(rtc_type *pTimeSrc);
	void time_sig_to_time(sig_time_type sig, rtc_type *pTimeSrc);

	int8_t AsciiToTime(uint8_t *Src, rtc_type *pTime);
	uint8_t TimeToAscii_YMDHMS(rtc_type *pTime, uint8_t *Des);
	uint8_t TimeToAscii_YMDHM(rtc_type *pTime, uint8_t *Des);
	uint8_t DateToAscii_YMD(rtc_type *pTime, uint8_t *Des);
	uint8_t ClockTimeToAscii_HMS(rtc_type *pTime, uint8_t *Des);
	uint8_t bcd_time_to_ascii(bcd_time_type bcd_time, uint8_t *des);

	uint16_t sig_time_to_ascii_ymdhm(sig_time_type sig, uint8_t *buff);		  
	uint16_t sig_time_to_ascii_ymdhms(sig_time_type sig, uint8_t *buff);	  
	uint16_t sig_time_to_ascii_ymd(sig_time_type sig, uint8_t *buff);	  
	uint16_t sig_time_to_ascii_hm(sig_time_type sig, uint8_t *buff);	  
	uint16_t sig_time_to_ascii_hms(sig_time_type sig, uint8_t *buff); 

	uint8_t sig_time_week_get(sig_time_type sigt);

	void rtc_year_base_set(int16_t year); 
	int16_t rtc_year_base_get(void);

#ifndef LIB_NO_TIME
	time_t sig_time_diff(sig_time_type start, sig_time_type end);
#endif

	uint8_t sig_time_to_tm(sig_time_type sig, struct tm *tim);
	uint8_t time_to_tm(rtc_type *time, struct tm *tim);

	int32_t sig_time_to_min(sig_time_type sig);
	
	int32_t sig_time_to_day(sig_time_type sig);
	
	int32_t sig_time_min_diff(sig_time_type sig1, sig_time_type sig2);
	
	int32_t sig_time_sec_diff(sig_time_type sig1, sig_time_type sig2);
	
	int32_t sig_time_sec2t_diff(sig_time_type sig1, sig_time_type sig2);

	int32_t ymd_time_to_day(ymd_time_type sig);
	int32_t ymd_time_day_diff(ymd_time_type ydm1, ymd_time_type ydm2);
	ymd_time_type sig_time_to_ymd(sig_time_type sig);
	sig_time_type ymd_time_to_sig(ymd_time_type ymd);

	void micro_year_base_set(int16_t year); 
	int16_t micro_year_base_get(void);
	micro_time_type time_sig_to_micro(sig_time_type sig);
	sig_time_type time_micro_to_sig(micro_time_type micro);
	micro_time_type time_rtc_to_micro(rtc_type *rtc);
	void time_micro_to_rtc(micro_time_type micro, rtc_type *rtc);
	uint16_t micro_time_to_ascii_ymdhms(micro_time_type micro, uint8_t *buff);
	uint16_t micro_time_to_ascii_ymdhm(micro_time_type micro, uint8_t *buff);
	uint16_t micro_time_to_ascii_ymd(micro_time_type micro, uint8_t *buff);
	uint16_t micro_time_to_ascii_hms(micro_time_type micro, uint8_t *buff);
	uint16_t micro_time_to_ascii_hm(micro_time_type micro, uint8_t *buff);
	int32_t micro_time_to_min(micro_time_type micro); 
	int32_t micro_time_to_day(micro_time_type micro); 
	int32_t micro_time_min_diff(micro_time_type rtc1, micro_time_type rtc2);
	int32_t micro_time_sec_diff(micro_time_type rtc1, micro_time_type rtc2);

	hms_time_type sec_to_hms(uint32_t sec);									 
	int8_t hms_to_ascii_hms(hms_time_type hms, char *des, uint8_t hour_len); 
	uint32_t hms_to_sec(hms_time_type hms);
	uint32_t hms_to_ms10(hms_time_type hms);
	int8_t sec_to_ascii_hms(uint32_t sec, char *des, uint8_t hour_len);

	int32_t asc_time_to_bin_s2(const char *str);

	uint32_t asc_time_to_bin_s3(const char *str);

	int32_t bin_time_to_asc_s2(int32_t tim, char *str, uint8_t h_len, uint8_t attr);

	uint32_t bin_time_to_asc_s3(int32_t tim, char *str, uint8_t h_len, uint8_t attr);
    
    void date_split_char_set(char split_char);

#ifdef __cplusplus
}
#endif

#endif 
